package com.yq.cp.service.yw.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yq.cp.entity.basics.OrganizationalStructure;
import com.yq.cp.entity.yw.YwZbgl;
import com.yq.cp.entity.yw.vo.YwZbglVo;
import com.yq.cp.mapper.yw.YwZbglMapper;
import com.yq.cp.service.yw.YwZbglService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 运维-值班管理 服务实现类
 * </p>
 *
 * @author zh
 * @since 2024-09-07
 */
@Service
public class YwZbglServiceImpl extends ServiceImpl<YwZbglMapper, YwZbgl> implements YwZbglService {

    @Resource
    YwZbglMapper ywZbglMapper;

    @Override
    public IPage<YwZbgl> all(YwZbglVo ywZbgl) {
        IPage<YwZbgl> iPage=new Page<>(ywZbgl.getPage(),ywZbgl.getSize());
        ywZbglMapper.page(iPage, ywZbgl);

        return iPage;
    }

    @Override
    public void add(OrganizationalStructure vo){
        Long nyzId = vo.getId();

        QueryWrapper<YwZbgl> queryWrapper2=new QueryWrapper<>();
        queryWrapper2.eq("nyz_id",nyzId);
        boolean exists = ywZbglMapper.exists(queryWrapper2);
        if (exists){
            return;
        }

        QueryWrapper<YwZbgl> queryWrapper1=new QueryWrapper<>();
        queryWrapper1.isNull("nyz_id");
        List<YwZbgl> ywZbgls1 = ywZbglMapper.selectList(queryWrapper1);


        for (int i = 0; i < ywZbgls1.size(); i++) {
            YwZbgl ywZbgl1 = ywZbgls1.get(i);
            ywZbgl1.setId(null);
            ywZbgl1.setQdsId(vo.getChannelId());
            ywZbgl1.setDqId(vo.getRegionId());
            ywZbgl1.setShengId(vo.getShengId());
            ywZbgl1.setShiId(vo.getShiId());
            ywZbgl1.setQuId(vo.getQuId());
            ywZbgl1.setNyzId(nyzId);
            ywZbgl1.setMaxNumber(0l);

            ywZbglMapper.insert(ywZbgl1);
        }
    }
}
